package com.spider.service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.spider.utils.ProcessUtils;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class GetHtml implements PageProcessor{

	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);
	private ProcessUtils processUtils = new ProcessUtils(); 
	private Matcher match;
	private Pattern pattern;
	
	
	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}
	
	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
//		this.proContent(page);
		page.putField("html", page.getHtml());
		System.out.println(page.getResultItems().get("html").toString());
	}
	
	public void proContent(Page page){
		page.putField("content", page.getHtml().xpath("//div[@class='u-bodyHtml'")
				.regex("/span&gt;.*").replace("/span&gt;", "")
				.replace("&lt;u&gt;&nbsp;&nbsp;&nbsp; &lt;/u&gt;", "____")
				.replace("&lt;sub&gt;&lt;/sub&gt;", "")
				.replace("&lt;sup&gt;&lt;/sup&gt;", "")
				.replace("&lt;i&gt;","").replace("&lt;/i&gt;","")
				.replace("</div>", "").all());
		
		this.proDiff(page);
		this.proId(page);
		
		String content = processUtils.processStr(page.getResultItems().get("content").toString());
		String[] contents = content.split("\n");
		//��ɴ���
		for (String cont : contents) {
//			question = new Question();
//			flagSize ++;
			String proceUrl = this.processUrl(cont);
			String proce = processUtils.processSub(processUtils.processSup(proceUrl));
			System.err.println(proce);
//			question.setContent(proce);
//			quesList.add(question);
		}
		
		this.proUrl(page);
	}
	
	public void proDiff(Page page){
		page.putField("diff",page.getHtml().xpath("//div[@class='f-fr']/b"));
		String diff = processUtils.processStr(page.getResultItems().get("diff").toString());
		System.out.println("diff:"+diff);
	}
	
	public void proId(Page page){
		page.putField("quesId", page.getHtml().xpath("//li[@class=question]"));
		String rowKey = processUtils.processStr(page.getResultItems().get("quesId").toString());
		System.out.println("rowKey:"+rowKey);
	}
	
	public void proUrl(Page page){
		List<String> urls = page.getHtml()
				.xpath("//div[@class='laypage_main laypageskin_default']")
				.links().regex(".*hdSearch=.*").all();
		page.addTargetRequests(urls);
	}
	
	//�滻imgUrlΪ<img>,���־û�imgUrl
	public String processUrl(String cont){
		String imgUrl = "";
		if(cont.contains("&lt;img")){
			pattern = pattern.compile("http.+png");
			match = pattern.matcher(cont);
			if(match.find()){
				imgUrl = match.group();
//				question.setImgUrl(imgUrl);
			}
			String s = cont.replaceAll("&lt;img.+src.+&gt;", "<img>");
			return s;
		}
		return cont;
	}
	
	public static void main(String[] args) {
		Spider.create(new QuesPageProcess())
		.addUrl("http://www.tiku.com/testPaper.html")
//		.addUrl("http://www.tiku.com/testPaper.html?sct=0&cn=%E6%95%B0%E5%AD%A6&st=2&cid=500004&bid=800009&vid=800005&uid=800004&cptid=700009&qtid=600050&difficulty=&sort=0")
		.thread(5)
		.run();
	}
}
